package org.esgi.repository.impl;

import java.io.File;

import java.util.ArrayList;
import java.util.List;

import org.esgi.model.impl.User;
import org.esgi.model.interfaces.IUser;

import org.esgi.repository.interfaces.IUserRepository;
import org.esgi.transformer.impl.XMLUserTransformer;
import org.esgi.util.StringEncoder;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.DocumentTraversal;
import org.w3c.dom.traversal.NodeFilter;
import org.w3c.dom.traversal.NodeIterator;

public class XMLUserRepository extends XMLRepository implements IUserRepository
{
	private File file_;
	private XMLUserTransformer userTransformer_;
	
	public XMLUserRepository(File file)
	{
		super();
		this.setFile(file);
		userTransformer_ = new XMLUserTransformer();
		userTransformer_.setDefaultNamespace("bmb");
	}	
	
	public File getFile() { return this.file_ ;}
	
	public void setFile(File file) { this.file_ = file; }
	
	@Override
	public Boolean addUser(IUser u) 
	{
		if(u != null)
		{
			File f = this.getFile();
			Document doc = this.parseFile(f);
			String login = u.getLogin();
			String pwd = u.getPassword();
			if(login != null && pwd != null)
			{
				if(this.nodeForUser(u, doc) == null)
				{
					Node n = this.userTransformer_.userToElement(u, doc);
					if(n != null)
					{
						doc.getDocumentElement().appendChild(n);
						return this.serializeDocument(doc, f);
					}
				}
			}
		}
		return false;
	}

	@Override
	public Boolean deleteUser(IUser u) 
	{
		File f = this.getFile();
		Document doc = this.parseFile(f);
		Node node = this.nodeForUser(u, doc);
		if(node != null)
		{
			node.getParentNode().removeChild(node);
			return this.serializeDocument(doc, f);
		}
		return false;
	}

	@Override
	public IUser getUser(String log, String pwd) 
	{
		return this.getUser(log, pwd, false);
	}

	@Override
	public IUser getUser(String log, String pwd, Boolean secured) 
	{
		File f = this.getFile();
		Document doc = this.parseFile(f);
		Node n = this.nodeForUser(new User(log), doc);
		if(n != null)
		{
			String secure_pwd = secured?pwd:StringEncoder.encode(pwd, StringEncoder.Algorithm.SHA1);
			IUser u = this.userTransformer_.userFromElement((Element) n);
			if(u.getPassword().equals(secure_pwd))
				return u;
		}
		return null;
	}

	@Override
	public List<IUser> getAllUsers() 
	{
		File f = this.getFile();
		Document doc = this.parseFile(f);
		DocumentTraversal dt = (DocumentTraversal)doc;
		NodeIterator it = dt.createNodeIterator(doc.getDocumentElement(), NodeFilter.SHOW_ALL, null, true);	
		Node n = null;
		List<IUser> users = new ArrayList<IUser>();				
		while((n=it.nextNode())!=null)
		{
			if(n.getNodeName().equals(this.userTransformer_.addDefaultNamespaceToName(XMLUserTransformer.XMLUserNodeName)))
				users.add(this.userTransformer_.userFromElement((Element) n));
		}
		return users;
	}
	
	private Node nodeForUser(IUser u, Document doc)
	{
		if(u != null && doc != null)
		{
			DocumentTraversal dt = (DocumentTraversal)doc;
			NodeIterator it = dt.createNodeIterator(doc.getDocumentElement(), NodeFilter.SHOW_ALL, new XMLUserNodeFilter(u), true);
			return it.nextNode();
		}
		return null;
	}
}
